/***
This do-file plots the evolution of the slope between the change of low-education
job postings and median 2 bedroom rent in the period 2020-2021.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"

* Create required subfolders
cap mkdir "${root}/results/Jobs"

*-------------------------------------------------------------------------------
* Load and process data
*-------------------------------------------------------------------------------

foreach year in 2020 2021 {
    forvalues i = 1/2 {
	preserve
    project, uses("${root}/data/web_${jobs_`year'_`i'}/data/Job Postings - County - Weekly.csv")
	import delimited "${root}/data/web_${jobs_`year'_`i'}/data/Job Postings - County - Weekly.csv", clear

	tempfile temp_`year'_`i'
	save `temp_`year'_`i''
	restore
    append using `temp_`year'_`i''
}
}

* Create date
gen date = mdy(month, day_endofweek, year)

* Re-format change variable
replace bg_posts_jzgrp12 = bg_posts_jzgrp12 * 100

* Calculate changes for each period
local i 4
foreach month in april may june july august september october november december {
	gegen temp = mean(bg_posts_jzgrp12) if year == 2020 & month == `i', by(countyfips)
	gegen change_`month'_2020 = mean(temp), by(countyfips)
	drop temp
	local i = `i' + 1

}

local i 1
foreach month in january february march april may june july august september october november december {
	gegen temp = mean(bg_posts_jzgrp12) if year == 2021 & month == `i', by(countyfips)
	gegen change_`month'_2021 = mean(temp) , by(countyfips)
	drop temp
	local i = `i' + 1
}

* Merge county-level covariates
rename countyfips county_fips
project, uses("${root}/data/derived/ACS 2014-2018 5-Year County/ACS 2014-2018 County.dta")
merge m:1 county_fips using "${root}/data/derived/ACS 2014-2018 5-Year County/ACS 2014-2018 County.dta", keep(1 3) nogen keepusing(med_2br_2014_2018_est pop_2014_2018_est medhhinc_2014_2018_est)
rename county_fips countyfips

rename (pop_2014_2018_est med_2br_2014_2018_est medhhinc_2014_2018_est) (pop_2018 median_2br_rent_2018 medhhinc_2018)

gcollapse change_* median_2br_rent_2018 pop_2018, by(countyfips)

* Run regressions
foreach month in april may june july august september october november december {
	reg change_`month'_2020 median_2br_rent_2018 [w = pop_2018], r
		local beta = _b[median_2br_rent_2018] * 1000
		local beta_`month'_2020: display %4.2f `beta'
		local std_err = _se[median_2br_rent_2018] * 1000
		local se_`month'_2020: display %4.2f `std_err'
		local lb_`month'_2020 = (_b[median_2br_rent_2018] - invttail(e(df_r), 0.025) * _se[median_2br_rent_2018]) * 1000
		local ub_`month'_2020 = (_b[median_2br_rent_2018] + invttail(e(df_r), 0.025) * _se[median_2br_rent_2018]) * 1000
}

foreach month in january february march april may june july august september october november december {
	reg change_`month'_2021 median_2br_rent_2018 [w = pop_2018], r
		local beta = _b[median_2br_rent_2018] * 1000
		local beta_`month'_2021: display %4.2f `beta'
		local std_err = _se[median_2br_rent_2018] * 1000
		local se_`month'_2021: display %4.2f `std_err'
		local lb_`month'_2021 = (_b[median_2br_rent_2018] - invttail(e(df_r), 0.025) * _se[median_2br_rent_2018]) * 1000
		local ub_`month'_2021 = (_b[median_2br_rent_2018] + invttail(e(df_r), 0.025) * _se[median_2br_rent_2018]) * 1000
}

*-------------------------------------------------------------------------------
* Create dataset with slope and SE for each month
*-------------------------------------------------------------------------------
clear
set obs 24

gen year = 2020 if _n <= 12
replace year = 2021 if year == .
gen month = .

forvalues m = 1/12 {
	replace month = `m' if _n == `m'
}

local i = 13
forvalues m = 1/12 {
	replace month = `m' if _n == `i'
	local i = `i' + 1
}

gen slope = `beta_april_2020' if year == 2020 & month == 4
replace slope = `beta_may_2020' if year == 2020 & month == 5
replace slope = `beta_june_2020' if year == 2020 & month == 6
replace slope = `beta_july_2020' if year == 2020 & month == 7
replace slope = `beta_august_2020' if year == 2020 & month == 8
replace slope = `beta_september_2020' if year == 2020 & month == 9
replace slope = `beta_october_2020' if year == 2020 & month == 10
replace slope = `beta_november_2020' if year == 2020 & month == 11
replace slope = `beta_december_2020' if year == 2020 & month == 12

replace slope = `beta_january_2021' if year == 2021 & month == 1
replace slope = `beta_february_2021' if year == 2021 & month == 2
replace slope = `beta_march_2021' if year == 2021 & month == 3
replace slope = `beta_april_2021' if year == 2021 & month == 4
replace slope = `beta_may_2021' if year == 2021 & month == 5
replace slope = `beta_june_2021' if year == 2021 & month == 6
replace slope = `beta_july_2021' if year == 2021 & month == 7
replace slope = `beta_august_2021' if year == 2021 & month == 8
replace slope = `beta_september_2021' if year == 2021 & month == 9
replace slope = `beta_october_2021' if year == 2021 & month == 10
replace slope = `beta_november_2021' if year == 2021 & month == 11
replace slope = `beta_december_2021' if year == 2021 & month == 12

gen lower_bound = `lb_april_2020' if year == 2020 & month == 4
replace lower_bound = `lb_may_2020' if year == 2020 & month == 5
replace lower_bound = `lb_june_2020' if year == 2020 & month == 6
replace lower_bound = `lb_july_2020' if year == 2020 & month == 7
replace lower_bound = `lb_august_2020' if year == 2020 & month == 8
replace lower_bound = `lb_september_2020' if year == 2020 & month == 9
replace lower_bound = `lb_october_2020' if year == 2020 & month == 10
replace lower_bound = `lb_november_2020' if year == 2020 & month == 11
replace lower_bound = `lb_december_2020' if year == 2020 & month == 12

replace lower_bound = `lb_january_2021' if year == 2021 & month == 1
replace lower_bound = `lb_february_2021' if year == 2021 & month == 2
replace lower_bound = `lb_march_2021' if year == 2021 & month == 3
replace lower_bound = `lb_april_2021' if year == 2021 & month == 4
replace lower_bound = `lb_may_2021' if year == 2021 & month == 5
replace lower_bound = `lb_june_2021' if year == 2021 & month == 6
replace lower_bound = `lb_july_2021' if year == 2021 & month == 7
replace lower_bound = `lb_august_2021' if year == 2021 & month == 8
replace lower_bound = `lb_september_2021' if year == 2021 & month == 9
replace lower_bound = `lb_october_2021' if year == 2021 & month == 10
replace lower_bound = `lb_november_2021' if year == 2021 & month == 11
replace lower_bound = `lb_december_2021' if year == 2021 & month == 12

gen upper_bound = `ub_april_2020' if year == 2020 & month == 4
replace upper_bound = `ub_may_2020' if year == 2020 & month == 5
replace upper_bound = `ub_june_2020' if year == 2020 & month == 6
replace upper_bound = `ub_july_2020' if year == 2020 & month == 7
replace upper_bound = `ub_august_2020' if year == 2020 & month == 8
replace upper_bound = `ub_september_2020' if year == 2020 & month == 9
replace upper_bound = `ub_october_2020' if year == 2020 & month == 10
replace upper_bound = `ub_november_2020' if year == 2020 & month == 11
replace upper_bound = `ub_december_2020' if year == 2020 & month == 12

replace upper_bound = `ub_january_2021' if year == 2021 & month == 1
replace upper_bound = `ub_february_2021' if year == 2021 & month == 2
replace upper_bound = `ub_march_2021' if year == 2021 & month == 3
replace upper_bound = `ub_april_2021' if year == 2021 & month == 4
replace upper_bound = `ub_may_2021' if year == 2021 & month == 5
replace upper_bound = `ub_june_2021' if year == 2021 & month == 6
replace upper_bound = `ub_july_2021' if year == 2021 & month == 7
replace upper_bound = `ub_august_2021' if year == 2021 & month == 8
replace upper_bound = `ub_september_2021' if year == 2021 & month == 9
replace upper_bound = `ub_october_2021' if year == 2021 & month == 10
replace upper_bound = `ub_november_2021' if year == 2021 & month == 11
replace upper_bound = `ub_december_2021' if year == 2021 & month == 12

gen se = `se_april_2020' if year == 2020 & month == 4
replace se = `se_may_2020' if year == 2020 & month == 5
replace se = `se_june_2020' if year == 2020 & month == 6
replace se = `se_july_2020' if year == 2020 & month == 7
replace se = `se_august_2020' if year == 2020 & month == 8
replace se = `se_september_2020' if year == 2020 & month == 9
replace se = `se_october_2020' if year == 2020 & month == 10
replace se = `se_november_2020' if year == 2020 & month == 11
replace se = `se_december_2020' if year == 2020 & month == 12

replace se = `se_january_2021' if year == 2021 & month == 1
replace se = `se_february_2021' if year == 2021 & month == 2
replace se = `se_march_2021' if year == 2021 & month == 3
replace se = `se_april_2021' if year == 2021 & month == 4
replace se = `se_may_2021' if year == 2021 & month == 5
replace se = `se_june_2021' if year == 2021 & month == 6
replace se = `se_july_2021' if year == 2021 & month == 7
replace se = `se_august_2021' if year == 2021 & month == 8
replace se = `se_september_2021' if year == 2021 & month == 9
replace se = `se_october_2021' if year == 2021 & month == 10
replace se = `se_november_2021' if year == 2021 & month == 11
replace se = `se_december_2021' if year == 2021 & month == 12

gen date = ym(year, month)
sort date

*-------------------------------------------------------------------------------
* Plot
*-------------------------------------------------------------------------------

tw (line slope date if slope != .) ///
   (line lower_bound date if lower_bound != ., lpattern(dash) lcolor(oi1%50)) ///
   (line upper_bound date if upper_bound != ., lpattern(dash) lcolor(oi1%50)), ///
	ytitle("Change in Low-Education Job Postings" "vs Rent Slope (%/$1000)") ///
	xtitle("") ///
	legend(order(2 "95% Confidence Interval") cols(1) pos(5) ring(0) size(small) symxsize(size)) ///
	ylab( 0 "0%" -10 "-10%" -20 "-20%" -30 "-30%" -40 "-40%", nogrid) ///
	yline(0, lpattern(dash) lcolor(black)) ///
	xlabel(`=ym(2020, 5)' `""May" "2020""'  ///
	  `=ym(2020, 7)' "Jul"  ///
	`=ym(2020, 9)' "Sep"  `=ym(2020, 11)' "Nov" ///
	`=ym(2021, 1)' `""Jan" "2021""'  ///
	 `=ym(2021, 3)' "Mar"  `=ym(2021, 5)' "May" ///
	 `=ym(2021, 7)' "Jul"  ///
	`=ym(2021, 9)' "Sep"  `=ym(2021, 11)' "Nov", labsize(medsmall))

oi_graph_export "${root}/results/Jobs/Jobs - Evolution of rent gradient - Low-Educ", type(${fig_type})

* Black and white version for QJE
tw (line slope date if slope != ., lcolor(gs0)) ///
   (line lower_bound date if lower_bound != ., lpattern(dash) lcolor(gs0%50)) ///
   (line upper_bound date if upper_bound != ., lpattern(dash) lcolor(gs0%50)), ///
	ytitle("Change in Low-Education Job Postings" "vs Rent Slope (%/$1000)") ///
	xtitle("") ///
	legend(order(2 "95% Confidence Interval") cols(1) pos(5) ring(0) size(small) symxsize(size)) ///
	ylab( 0 "0%" -10 "-10%" -20 "-20%" -30 "-30%" -40 "-40%", nogrid) ///
	yline(0, lpattern(dash) lcolor(black)) ///
	xlabel(`=ym(2020, 5)' `""May" "2020""'  ///
	  `=ym(2020, 7)' "Jul"  ///
	`=ym(2020, 9)' "Sep"  `=ym(2020, 11)' "Nov" ///
	`=ym(2021, 1)' `""Jan" "2021""'  ///
	 `=ym(2021, 3)' "Mar"  `=ym(2021, 5)' "May" ///
	 `=ym(2021, 7)' "Jul"  ///
	`=ym(2021, 9)' "Sep"  `=ym(2021, 11)' "Nov", labsize(medsmall))

graph export "${root}/results/QJE_Figures_BlackAndWhite/Figure_6c.svg", replace
project, creates("${root}/results/QJE_Figures_BlackAndWhite/Figure_6c.svg")
